2000W / 48V LLC Battery-Charger  V1.00
system_XMC1400.c
Go to the documentation of this file.
1 /*********************************************************************************************************************
2  * @file system_XMC1400.c
3  * @brief Device specific initialization for the XMC1400-Series according to CMSIS
4  * @version V1.2
5  * @date 19 Jun 2017
6  *
7  * @cond
8  *********************************************************************************************************************
9  * Copyright (c) 2015-2017, Infineon Technologies AG
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
13  * following conditions are met:
14  *
15  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16  * disclaimer.
17  *
18  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided with the distribution.
20  *
21  * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
33  * Infineon Technologies AG dave@infineon.com).
34  *********************************************************************************************************************
35  *
36  * *************************** Change history ********************************
37  * V1.0, 03 Sep 2015, JFT : Initial version
38  * MCLK = 48MHz, PCLK = 96MHz
39  * V1.1, 09 Dec 2015, JFT : Enable prefetch unit
40  * V1.2, 19 Jun 2017, Rely on cmsis_compiler.h instead of defining __WEAK
41  * Added support for ARM Compiler 6 (armclang)
42  *
43  * @endcond
44  */
45 
46 /*******************************************************************************
47  * HEADER FILES
48  *******************************************************************************/
49 
50 #include <XMC1400.h>
51 #include "system_XMC1400.h"
52 
53 /*******************************************************************************
54  * MACROS
55  *******************************************************************************/
56 #define DCO1_FREQUENCY (48000000U)
57 
58 /*
59 //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
60 */
61 
62 /*
63 // <h> Clock configuration
64 */
65 
66 /*
67 // <o> External crystal frequency [Hz]
68 // <8000000=> 8MHz
69 // <12000000=> 12MHz
70 // <16000000=> 16MHz
71 // <20000000=> 20MHz
72 // <i> Defines external crystal frequency
73 // <i> Default: 20MHz
74 */
75 #define OSCHP_FREQUENCY (20000000U)
76 
77 /*
78 // <o> DCLK clock source selection
79 // <0=> Internal oscillator DCO1 (48MHz)
80 // <1=> External crystal oscillator
81 // <i> Default: Internal oscillator DCO1 (48MHz)
82 */
83 #define DCLK_CLOCK_SRC 0
84 #define DCLK_CLOCK_SRC_DCO1 0
85 #define DCLK_CLOCK_SRC_EXT_XTAL 1
86 
87 /*
88 // <o> OSCHP external oscillator mode
89 // <0=> Crystal mode
90 // <1=> External clock direct input mode
91 // <i> Default: Crystal mode
92 */
93 #define OSCHP_MODE 0
94 #define OSCHP_MODE_XTAL 0
95 #define OSCHP_MODE_DIRECT 1
96 
97 /*
98 // <o> RTC clock source selection
99 // <0=> Internal oscillator DCO2 (32768Hz)
100 // <5=> External crystal oscillator
101 // <i> Default: Internal oscillator DCO2 (32768Hz)
102 */
103 #define RTC_CLOCK_SRC 0
104 #define RTC_CLOCK_SRC_DCO2 0
105 #define RTC_CLOCK_SRC_EXT_XTAL 5
106 /*
107 // <o> PCLK clock source selection
108 // <0=> MCLK
109 // <1=> 2xMCLK
110 // <i> Default: 2xMCLK
111 */
112 #define PCLK_CLOCK_SRC 1
113 #define PCLK_CLOCK_SRC_MCLK 0
114 #define PCLK_CLOCK_SRC_2XMCLK 1
115 
116 /*
117 //-------- <<< end of configuration section >>> ------------------
118 */
119 
120 /*******************************************************************************
121  * GLOBAL VARIABLES
122  *******************************************************************************/
123 
124 #if defined ( __CC_ARM )
125 uint32_t SystemCoreClock __attribute__((at(0x20003FFC)));
126 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
127 uint32_t SystemCoreClock __attribute__((section(".ARM.__at_0x20003FFC")));
128 #elif defined ( __ICCARM__ )
129 __no_init uint32_t SystemCoreClock;
130 #elif defined ( __GNUC__ )
131 uint32_t SystemCoreClock __attribute__((section(".no_init")));
132 #elif defined ( __TASKING__ )
133 uint32_t SystemCoreClock __at( 0x20003FFC );
134 #endif
135 
136 /*******************************************************************************
137  * LOCAL FUNCTIONS
138  *******************************************************************************/
139 #if DCLK_CLOCK_SRC != DCLK_CLOCK_SRC_DCO1
140 static inline void delay(uint32_t cycles)
141 {
142  while(cycles > 0)
143  {
144  __NOP();
145  cycles--;
146  }
147 }
148 #endif
149 
150 /*******************************************************************************
151  * API IMPLEMENTATION
152  *******************************************************************************/
153 
154 __WEAK void SystemInit(void)
155 {
156  SystemCoreSetup();
158 }
159 
160 __WEAK void SystemCoreSetup(void)
161 {
162  /* Enable Prefetch unit */
163  SCU_GENERAL->PFUCR &= ~SCU_GENERAL_PFUCR_PFUBYP_Msk;
164 }
165 
166 __WEAK void SystemCoreClockSetup(void)
167 {
168  /* Clock setup done during SSW using the CLOCK_VAL1 and CLOCK_VAL2 defined in vector table */
169 
170  /* disable bit protection */
171  SCU_GENERAL->PASSWD = 0x000000C0UL;
172 
173 #if DCLK_CLOCK_SRC != DCLK_CLOCK_SRC_DCO1
174 
175  if (OSCHP_GetFrequency() > 20000000U)
176  {
177  SCU_ANALOG->ANAOSCHPCTRL |= SCU_ANALOG_ANAOSCHPCTRL_HYSCTRL_Msk;
178  }
179 
180  /* OSCHP source selection - OSC mode */
181  SCU_ANALOG->ANAOSCHPCTRL = (SCU_ANALOG->ANAOSCHPCTRL & ~SCU_ANALOG_ANAOSCHPCTRL_MODE_Msk) |
182  (OSCHP_MODE << SCU_ANALOG_ANAOSCHPCTRL_MODE_Pos);
183 
184  /* Enable OSC_HP oscillator watchdog*/
185  SCU_CLK->OSCCSR |= SCU_CLK_OSCCSR_XOWDEN_Msk;
186 
187  do
188  {
189  /* Restart OSC_HP oscillator watchdog */
190  SCU_INTERRUPT->SRCLR1 = SCU_INTERRUPT_SRCLR1_LOECI_Msk;
191  SCU_CLK->OSCCSR |= SCU_CLK_OSCCSR_XOWDRES_Msk;
192 
193  /* Wait a few DCO2 cycles for the update of the clock detection result */
194  delay(2500);
195 
196  /* check clock is ok */
197  }
198  while(SCU_INTERRUPT->SRRAW1 & SCU_INTERRUPT_SRRAW1_LOECI_Msk);
199 
200  /* DCLK source using OSC_HP */
201  SCU_CLK->CLKCR1 |= SCU_CLK_CLKCR1_DCLKSEL_Msk;
202 
203 #else
204 
205  /* DCLK source using DCO1 */
206  SCU_CLK->CLKCR1 &= ~SCU_CLK_CLKCR1_DCLKSEL_Msk;
207 
208 #endif
209 
210 #if RTC_CLOCK_SRC == RTC_CLOCK_SRC_EXT_XTAL
211  /* Enable OSC_LP */
212  SCU_ANALOG->ANAOSCLPCTRL &= ~SCU_ANALOG_ANAOSCLPCTRL_MODE_Msk;
213 #endif
214 
215  /* Update PCLK selection mux. */
216  /* Fractional divider enabled, MCLK frequency equal DCO1 frequency or external crystal frequency */
217  SCU_CLK->CLKCR = (1023UL <<SCU_CLK_CLKCR_CNTADJ_Pos) |
218  (RTC_CLOCK_SRC << SCU_CLK_CLKCR_RTCCLKSEL_Pos) |
219  (PCLK_CLOCK_SRC << SCU_CLK_CLKCR_PCLKSEL_Pos) |
220  0x100U; /* IDIV = 1 */
221 
222  /* enable bit protection */
223  SCU_GENERAL->PASSWD = 0x000000C3UL;
224 
226 }
227 
228 __WEAK void SystemCoreClockUpdate(void)
229 {
230  static uint32_t IDIV, FDIV;
231 
232  IDIV = ((SCU_CLK->CLKCR) & SCU_CLK_CLKCR_IDIV_Msk) >> SCU_CLK_CLKCR_IDIV_Pos;
233 
234  if (IDIV != 0)
235  {
236  FDIV = ((SCU_CLK->CLKCR) & SCU_CLK_CLKCR_FDIV_Msk) >> SCU_CLK_CLKCR_FDIV_Pos;
237  FDIV |= ((SCU_CLK->CLKCR1) & SCU_CLK_CLKCR1_FDIV_Msk) << 8;
238 
239  /* Fractional divider is enabled and used */
240  if (((SCU_CLK->CLKCR1) & SCU_CLK_CLKCR1_DCLKSEL_Msk) == 0U)
241  {
242  SystemCoreClock = ((uint32_t)((DCO1_FREQUENCY << 6U) / ((IDIV << 10) + FDIV))) << 4U;
243  }
244  else
245  {
246  SystemCoreClock = ((uint32_t)((OSCHP_GetFrequency() << 6U) / ((IDIV << 10) + FDIV))) << 4U;
247  }
248  }
249  else
250  {
251  /* Fractional divider bypassed. */
252  if (((SCU_CLK->CLKCR1) & SCU_CLK_CLKCR1_DCLKSEL_Msk) == 0U)
253  {
254  SystemCoreClock = DCO1_FREQUENCY;
255  }
256  else
257  {
258  SystemCoreClock = OSCHP_GetFrequency();
259  }
260  }
261 }
262 
263 __WEAK uint32_t OSCHP_GetFrequency(void)
264 {
265  return OSCHP_FREQUENCY;
266 }
#define OSCHP_MODE
__WEAK void SystemCoreClockSetup(void)
__WEAK uint32_t OSCHP_GetFrequency(void)
__WEAK void SystemInit(void)
__WEAK void SystemCoreSetup(void)
#define OSCHP_FREQUENCY
#define RTC_CLOCK_SRC
#define PCLK_CLOCK_SRC
__WEAK void SystemCoreClockUpdate(void)
#define DCO1_FREQUENCY